package lc.二叉树.剑指Offer;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/11/2
 */
public class 剑指Offer33_二叉搜索树的后序遍历序列 {

    public boolean verifyPostorder(int[] postorder) {

        return recur(postorder,0,postorder.length - 1);
    }

    private boolean recur(int[] postorder, int l, int r) {
        if (l >= r) return true;
        int p = l;
        while (postorder[p] < postorder[r]) p++;
        int m = p;
        while (postorder[p] > postorder[r]) p++;
        return p == r && recur(postorder, l, m - 1) && recur(postorder, m , r - 1);
    }
}
